#include "UserManager.h"
#include <chrono>
#include <ctime>

UserManager::UserManager()
    : id(0), username(""), password(""), role(""), is_active(1), is_deleted(0),
      created_on(getCurrentTime()), updated_on(getCurrentTime()) {}

void UserManager::showUserInfo()
{
    cout << "id: " << id << endl;
    cout << "username: " << username << endl;
    cout << "role: " << role << endl;
    cout << "is_active: " << is_active << endl;
    cout << "is_deleted: " << is_deleted << endl;
    cout << "created_on: " << created_on << endl;
}

string UserManager::getCurrentTime()
{
    auto now = chrono::system_clock::to_time_t(chrono::system_clock::now());
    struct tm *timeinfo = localtime(&now);
    char buffer[80];
    strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", timeinfo);
    return string(buffer);
}

// 登录
int UserManager::login(UserManager *user)
{
    DatabaseManager db;
    int result = db.getUser(this, user);
    if (result != 0)
    {
        cout << "登录失败" << endl;
        return -1;
    }
    string sha256_password = SHA256::hash(user->getPassword());
    if (this->getPassword() != sha256_password)
    {
        cout << "密码错误" << endl;
        return -1;
    }
    return 0;
}